//index.js
Page({
    data: {
        // 轮播数据
        wheelData: [],
        // 热门活动
        hotActivityData: [],
        // 热门课程
        hotCourseData: [],
        // 免费课程
        freeCourseData: [],
        // 推荐商品
        goodGoodsData: [],
        // 优惠券列表
        couponData: [],
        // 是否显示优惠券
        isShowCouponPopup: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function() {
        this.onPullDownRefresh();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        Promise.all([
            this.loadWheelData(),
            this.loadHotActivityData(),
            this.loadHotCourseData(),
            this.loadFreeCourseData(),
            this.loadGoodsList(),
            this.loadCouponList(),
        ]).finally(() => {
            wx.stopPullDownRefresh();
        });
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        const image = "";
        return {
            imageUrl: image,
            path: this.route
        }
    },

    onWheelToTap: function(e) {
        const dataset = e.currentTarget.dataset,
            index = dataset.index;
        const item = this.data.wheelData[index];
        if (item.type.value == 10) {
            wx.navigateTo({
                url: '/pages/mall/goods-detail/goods-detail?id=' + item.goods_id,
            });
        } else if (item.type.value == 20) {
            wx.navigateTo({
                url: '/pages/course/detail/detail?id=' + item.goods_id,
            });
        } else if (item.type.value == 30) {
            wx.navigateTo({
                url: '/pages/activity/detail/detail?id=' + item.goods_id,
            });
        }
    },

    // 加载轮播
    loadWheelData: function() {
        return wx.http.get('api/index/wheel', {
            display_type: 10
        }).then((res) => {
            res = res.data.data.getList;
            this.setData({
                wheelData: res,
            });
            return res;
        }, (err) => {
            console.warn("api/index/wheel", err)
        });
    },

    // 加载优惠券
    loadCouponList: function() {
        return wx.http.get('api/coupon/lists', {}).then((res) => {
            res = res.data.data.list;
            this.setData({
                couponData: res,
                isShowCouponPopup: true,
            });
            return res;
        }, (err) => {
            console.warn("api/coupon/lists", err)
        });
    },

    // 领取优惠券
    receiveCoupon: function(e) {
        const dataset = e.currentTarget.dataset,
            id = dataset.id;

        return wx.http.get('api/user.coupon/receive', {
            coupon_id: id,
        }, {
            showLoading: true
        }).then((res) => {
            wx.showToast({
                title: '已领取！'
            });
            return res;
        }, (err) => {
            console.warn("api/user.coupon/receive", err)
        });
    },

    // 加载热门活动
    loadHotActivityData: function() {
        return wx.http.get('api/activity/lists', {}).then((res) => {
            res = res.data.data.categoryList;
            this.setData({
                hotActivityData: res.data,
            });
            return res.data;
        }, (err) => {
            console.warn("api/activity/lists", err)
        });
    },

    // 加载热门课程
    loadHotCourseData: function() {
        return wx.http.get('api/curriculum/lists', {
            page: 1,
            type: 'popular',
            sortType: 'all',
            sortPrice: 0,
            search: '',
            course_id: '',
            category_id: '',
        }).then((res) => {
            res = res.data.data.list;
            this.setData({
                hotCourseData: res.data,
            });
            return res.data;
        }, (err) => {
            console.warn("api/curriculum/lists", err)
        });
    },

    // 加载免费课程
    loadFreeCourseData: function() {
        return wx.http.get('api/curriculum/lists', {
            page: 1,
            type: 'free',
            sortType: 'all',
            sortPrice: 0,
            search: '',
            course_id: '',
            category_id: '',
        }).then((res) => {
            res = res.data.data.list;
            this.setData({
                freeCourseData: res.data,
            });
            return res.data;
        }, (err) => {
            console.warn("api/curriculum/lists", err)
        });
    },

    // 加载推荐商品列表
    loadGoodsList: function(page) {
        return wx.http.get('api/goods/home', {}).then((res) => {
            res = res.data.data.list;
            const hasMore = res.data.length >= res.per_page;
            const goodsList = page > 1 ? this.data.goodsList.concat(res.data) : res.data;
            this.setData({
                page: page,
                hasMore: hasMore,
                goodGoodsData: goodsList,
            });
            return res.data;
        }, (err) => {
            console.warn("api/goods/home", err)
        });
    },
});